package com.tianqu.tms.www.transfer.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAjaxAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.dto.CityDto;
import com.tianqu.tms.www.transfer.controller.QueryVaoNameAjaxController;

public class VaoNameAjaxAction extends TmsAjaxAction {
    private Logger logger = Logger.getLogger(SeniorSearchPrintAjaxAction.class);
    @Override
    public void process(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response,
            UserContext uc) throws TmsException {

        logger.debug(this.getClass().getName() + "#execute was called.");
//        String vaoName = request.getParameter("vaoName");   
        String vaoName = request.getParameter("query");
        if (vaoName == null) {
            responseValues(response, "", TEXT);
            return;
        }
        QueryVaoNameAjaxController controller = new QueryVaoNameAjaxController();
        try {
            uc.setAttribute("vaoName", vaoName);
            controller.execute(uc);
            
            List<CityDto> cityList = (List<CityDto>)uc.getAttribute("vaoList");
            List<Map<String,String>> list = new ArrayList<Map<String,String>>();
            Map<String,String> map = null;
            for (CityDto city:cityList) {
                map = new HashMap<String,String>();
                map.put("value", city.getVaoName());
                map.put("data", city.getProName() + ":"+city.getCityName());
                list.add(map);
            }

             JSONArray jsonArray =  JSONArray.fromObject(JSONArray.fromObject(list));

             JSONObject json =new JSONObject(); 
//             json.put("query", query);
             json.put("suggestions", jsonArray);
             
            responseValues(response, json.toString(), TEXT);
            
            
//            JSONArray jsonArray =  JSONArray.fromObject(JSONArray.fromObject(list));

//            JSONObject json =new JSONObject(); 
//            json.put("citys", jsonArray);
            
//           responseValues(response, json.toString(), TEXT);
        } catch (Exception e) {
            logger.error(e);
        }
    }

}
